<template>
  <basic-container>
    <div class="avue-crud">
      <!-- 搜索工作栏 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="设备名" prop="deviceName">
          <el-input v-model="queryParams.deviceName" placeholder="请输入设备名" clearable
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="50" style="margin-left: 20px;margin-right: 20px">
        <el-col :span="6" v-for="(item,index) in dataList" :key="index">
          <div style="padding: 14px">
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span>{{ item.deviceName }}</span>
                <el-switch
                  :disabled="item.onlineStatus === 0"
                  style="float: right; padding: 3px 0"
                  v-model="item.switchStatus"
                  active-text="开"
                  inactive-text="关"
                  active-value="on"
                  inactive-value="off"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  @change="switchChange($event,item)">
                </el-switch>
              </div>
              <span style="font-size: 15px; font-weight: bold">设备id：</span>
              <span style="font-size: 15px;">{{ item.deviceId }}</span>
              <br/>
              <span style="font-size: 15px; font-weight: bold">设备名称：</span>
              <span style="font-size: 15px;">{{ item.deviceName }}</span>
              <br/>
              <span style="font-size: 15px; font-weight: bold">设备MAC：</span>
              <span style="font-size: 15px;">{{ item.deviceMac }}</span>
              <br/>
              <span style="font-size: 15px; font-weight: bold">在线状态：</span>
              <dict-tag :type="DICT_TYPE.IOT_ONLINE_STATUS" :value="item.onlineStatus"/>
              <br/>
              <span style="font-size: 15px; font-weight: bold">设备上线时间：</span>
              <span style="font-size: 15px;">{{ item.onlineTime }}</span>
              <br/>
              <span style="font-size: 15px; font-weight: bold">设备接入状态：</span>
              <dict-tag :type="DICT_TYPE.DEVICE_ACCESS_STATUS" :value="item.accessStatus"/>
              <br/>
              <span style="font-size: 15px; font-weight: bold">设备接入日期：</span>
              <span style="font-size: 15px;">{{ item.accessDate }}</span>

            </el-card>

          </div>
        </el-col>
      </el-row>
      <!-- 分页组件 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
                  @pagination="getDataList"/>
    </div>
  </basic-container>
</template>

<script>
import {mapGetters} from "vuex";
import {addObj, delObj, fetchList, getObj, putObj, changeSwitch} from "@/api/iot/business-iot-device";

export default {
  name: "card",
  data() {
    return {
      switchValue: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        deviceName: null
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      dataListLoading: false,
      addOrUpdateVisible: false,
      dataList: []
    }
  },
  created() {
    this.getDataList()
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {

    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      fetchList(Object.assign({
        current: this.queryParams.current,
        size: this.pageSize
      })).then(response => {
        this.dataList = response.data.data.records
        this.total = response.data.data.total
      })
      this.dataListLoading = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getDataList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加 OAuth2 客户端";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getObj(id).then(response => {
        this.form = response.data.data;
        this.open = true;
        this.title = "修改 OAuth2 客户端";
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          putObj(this.form).then(response => {
            this.$message.success("修改成功");
            this.open = false;
            this.getDataList();
          });
          return;
        }
        // 添加的提交
        addObj(this.form).then(response => {
          this.$message.success("新增成功");
          this.open = false;
          this.getDataList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$confirm('是否确认删除设备编号为"' + row.clientId + '"的数据项?').then(function () {
        return delObj(id);
      }).then(() => {
        this.getDataList();
        this.$message.success("删除成功");
      }).catch(() => {
      });
    },
    switchChange(newVal, row) {
      console.log("开关新值", newVal)
      console.log("row", row)
      let params = {
        deviceId: row.deviceId,
        action: newVal
      }
      changeSwitch(params).then(res => {
        console.log("开关结果", res)
        this.getDataList();
      })
    }
  }
}
</script>

<style>
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  width: 700px;
}
</style>
